<template>
	
	 <el-dialog v-model="dialogVisible"
	  :title="dialogTitle"
	   :before-close="closeDialog"
		class="elDiaLog"
		lock-scroll width="1000px" 
		:close-on-click-modal="false" 
		:close-on-press-escape="false">
		<div class="form-box list-info" v-if="['act_edit','act_add'].includes(dialogStatus)" v-loading="loading2">
			<el-form  ref="dataFormRef" :model="formData" :rules="rules" label-width="150px">
				<el-form-item required label="所属工程" prop="engine_id">
					<div  v-if="formData.engine_id" >
						<el-text style="margin-right: 10px;" type="primary">{{selectRow.name}}</el-text>
						<el-button @click="bindGongCheng"  circle>
							<template #icon><i-ep-position /></template>
						</el-button>
					</div>
					<el-button v-else type="primary" @click="bindGongCheng">绑定工程</el-button>
				</el-form-item>
				<el-form-item required label="主体单位" prop="main_name">
					<el-input
					    v-model="formData.main_name"
					    placeholder="请输入主体单位"
					 />
				</el-form-item>
				<el-form-item required label="建设运行单位" prop="jianshe">
					<el-input
					    v-model="formData.jianshe"
					    placeholder="请输入建设运行单位"
					 />
				</el-form-item>
				<el-form-item required label="编制对接单位" prop="bianzhi">
					<el-input
					    v-model="formData.bianzhi"
					    placeholder="请输入编制对接单位"
					 />
				</el-form-item>
				<el-form-item required label="项目类别" prop="type">
					 <el-select
					      v-model="formData.type"
					      placeholder="请选择项目类别"
					      class="defalut-width"
					    >
					      <el-option
					        v-for="item in BUSINESS_TYPE"
					        :key="item.name"
					        :label="item.name"
					        :value="item.name"
					      />
					    </el-select>
				</el-form-item>
				<el-form-item required label="送审价" prop="songshen">
					<el-input
					    v-model="formData.songshen"
					    placeholder="请输入送审价"
					>
					 <template #append>元</template>
					 </el-input>
				</el-form-item>
				<el-form-item required label="财政评审单位" prop="pingshen">
					<el-input
					    v-model="formData.pingshen"
					    placeholder="请输入财政评审单位"
					>
					
					 </el-input>
				</el-form-item>
				<el-form-item required label="审定价" prop="shending">
					<el-input
					    v-model="formData.shending"
					    placeholder="请输入审定价"
					 >
					  <template #append>元</template>
					  </el-input>
				</el-form-item>
				<el-form-item required label="审増审减额" prop="difference">
					<el-input
					    v-model="formData.difference"
					    placeholder="请输入审増审减额"
					 >
					  <template #append>元</template>
					  </el-input>
				</el-form-item>
				<el-form-item required label="中标价" prop="zhongbiao">
					<el-input
					    v-model="formData.zhongbiao"
					    placeholder="请输入中标价"
					>
					 <template #append>元</template>
					 </el-input>
				</el-form-item>
				<el-form-item  label="审核日期" prop="check_date">
					<el-date-picker
					style="width: 100%;"
					        v-model="formData.check_date"
					        type="date"
							format="YYYY-MM-DD"
							value-format="YYYY-MM-DD"
					        placeholder="请选择审核日期"
					      />
				</el-form-item>
				<el-form-item required label="是否已签订委托合同" prop="is_weituo">
					 <el-radio-group v-model="formData.is_weituo">
					 	<el-radio :value="0" size="large">否</el-radio>
					 	<el-radio :value="1" size="large">是</el-radio>
					 </el-radio-group>
				</el-form-item>
				<el-form-item  label="备注" prop="mark">
					 <el-input
					    v-model="formData.mark"
					    :rows="6"
					    type="textarea"
					    placeholder="备注"
						maxlength="200"
						resize="none"
						show-word-limit
					  />
				</el-form-item>
			</el-form>
		</div>
		<div class="form-box" v-if="['act_info'].includes(dialogStatus)" v-loading="loading2">
			<el-descriptions size="default" :column="1" border>
				<el-descriptions-item label-class-name="labelC1"
					label="工程名称：">{{dataInfo.engine?.name}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="主体单位：">{{dataInfo.main_name}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="建设运行单位：">{{dataInfo.jianshe}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="编制对接单位：">{{dataInfo.bianzhi}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="项目类别：">{{dataInfo.type}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="送审价：">{{dataInfo.songshen}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="财政评审单位：">{{dataInfo.pingshen}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="审定价：">{{dataInfo.shending}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="审増审减额：">{{dataInfo.difference}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="中标价：">{{dataInfo.zhongbiao}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="审核日期：">{{dataInfo.check_date}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="是否已签订委托合同：">
						{{dataInfo.is_weituo==1?'是':'否'}}</el-descriptions-item>
				<el-descriptions-item label-class-name="labelC1"
					label="备注：">{{dataInfo.mark}}</el-descriptions-item>
			</el-descriptions>
		</div>
		<template #footer>
			<div class="dialog-footer">
				
				<el-button v-if="['act_info'].includes(dialogStatus)" :disabled="loading3" @click="closeDialog">关
					闭</el-button>
				<el-button v-if="['act_edit','act_add'].includes(dialogStatus)" type="primary" :loading="loading3"
					@click="handleSubmit">{{dialogStatus == 'act_edit'?'修改':'确定'}}</el-button>
				<el-button v-if="['act_edit','act_add'].includes(dialogStatus)" :disabled="loading3"
					@click="closeDialog">取 消</el-button>

			</div>
		</template>
		
		<!-- 绑定工程 -->
		<engineSelect
		   v-if="binddialogSelect.visible"
		   v-model="binddialogSelect.visible"
		  :dialogTitle="binddialogSelect.title"
		  @selectRowEngin="selectRowEngin"
		></engineSelect>
		
	</el-dialog>
	
	
</template>
<script setup lang="ts">
	import { useRoute } from 'vue-router';
	import _ from 'lodash-es'
	import { feilType } from "@/utils/funcall"
	import { useBaseConfigStore } from "@/store/modules/baseConfig";
	const baseConfigStore = useBaseConfigStore();
	import {BUSINESS_TYPE} from "@/utils/constant";
	
	import engineSelect  from "./engineSelect.vue"
	
	// 获取接口地址
	import {
		addLedger,
		updataLedger,
		getLedgerDetail,
	} from "@/api/aggravate";
	
	const props = defineProps({
		//数据列表索引值  例如 user ->对应着 vuex  == user_cxxxxx模块
	    dialogTitle: {
	        type: String,
	        default:"添加信息"
	    },
		dialogStatus: {
			type: String,
			default: "act_add"
		},
		dialogTableValue:{
		  type:Object,
		  default: ()=>{},
		}
	});

	const emit = defineEmits(['update:modelValue','initRefreshList']);
	const dialogVisible = ref(false);
	const loading2 = ref(false);//新增或者修改加载load按钮
	const loading3 = ref(false);//新增或者修改加载load按钮
	// 数据的data-ref
	const dataFormRef = ref(ElForm);
	const formData = ref<any>({
		id:undefined,
		engine_id:"",//所属工程
		main_name:"",//主体单位
		jianshe:"",//建设运营单位
		bianzhi:"",//编制对接单位
		type:"",//项目类别
		songshen:"",//送审价
		pingshen:"",//财政评审价
		shending:"",//审定价
		difference:"",//审增审减额
		zhongbiao:"",//中标价
		check_date:"",//审核日期
		is_weituo:"",//是否威拓
		mark:""
	})
	const dataInfo =ref<any>({});
	const selectRow= ref<any>({
		id:"",
		name:""
		
	});
	watch(
		()=>props.dialogTableValue,
		()=>{
			if (props.dialogStatus == "act_info") {
				console.log('查看详情')
				let deepItem = _.cloneDeep(props.dialogTableValue)
				getAuditDetail(deepItem);
			}
			else if (props.dialogStatus == "act_edit"){
				 let deepItem = _.cloneDeep(props.dialogTableValue)
				 getAuditDetail(deepItem); 
			}
			else
			{
				console.log('添加信息')
			}
	},{deep:true,immediate:true})
	
	const rules = reactive<any>({
		engine_id:[{ required: true, message: '请先绑定工程', trigger: 'change' }],
		main_name:[{ required: true, message: '请输入主体单位', trigger: 'blur' }],
		jianshe:[{ required: true, message: '请输入建设运行单位', trigger: 'blur' }],
		bianzhi:[{ required: true, message: '请输入编制对接单位', trigger: 'blur' }],
		type:[{ required: true, message: '请选择项目类别', trigger: 'change' }],
		songshen:[{ required: true, message: '请输入送审价', trigger: 'blur' }],
		pingshen:[{ required: true, message: '请输入财政评审单位', trigger: 'blur' }],
		shending:[{ required: true, message: '请输入审定价', trigger: 'blur' }],
		difference:[{ required: true, message: '请输入审増审减额', trigger: 'blur' }],
		zhongbiao:[{ required: true, message: '请输入中标价', trigger: 'blur' }],
		is_weituo:[{ required: true, message: '请选择是否已签订委托合同', trigger: 'change' }],
	})
	
	// users: [{ required: true, message: '请选择审批人', trigger: 'blur' }],
	// 提交新增内容/or修改
	function handleSubmit() {
		dataFormRef.value.validate((valid : boolean) => {
			console.log(valid)
			if (valid) {
				loading3.value = true;
				if(formData.value.id)
				{
					updataLedger(formData.value).then((res) => {
						console.log(res)
						emit('initRefreshList');
						loading3.value = false;
						ElMessage({ message: '修改成功', type: 'success' })
						closeDialog();
					}).finally(() => {
						loading3.value = false;
					})
				}
				else
				{
					addLedger(formData.value).then((res) => {
						console.log(res)
						emit('initRefreshList');
						loading3.value = false;
						ElMessage({ message: '添加成功', type: 'success' })
						closeDialog();
					}).finally(() => {
						loading3.value = false;
					})
					
				}
			}
		});
	}
	// 关闭弹窗
	function closeDialog() {
		 if (!loading3.value) {
			emit('update:modelValue',false)
		 }
	}
	
	const binddialogSelect = reactive<any>({
		title: "选择工程", //弹窗标题
		visible: false,   //弹窗显示or隐藏
		status:"add_art", //弹窗状态类型  :添加 add_art 编辑 act_edit 详情 detail_art  
		value:{}   		  //携带整条参数 row
	});
	// 绑定工程
	function bindGongCheng(){
		binddialogSelect.visible = true;
	}
	// 工程的回调函数
	function selectRowEngin(row){
		console.log(row)
		selectRow.value  = row;
		formData.value.engine_id = row.id;
		
	}
	// 获取详情
	function getAuditDetail(row){
		loading2.value = true;
		getLedgerDetail({id:row.id}).then((res) => {
			let data = res.data;
			formData.value.id = data.id;
			formData.value.engine_id =  data.engine_id;
			formData.value.main_name =  data.main_name;
			formData.value.jianshe =  data.jianshe;
			formData.value.bianzhi =  data.bianzhi;
			formData.value.type =  data.type;
			formData.value.songshen =  data.songshen;
			formData.value.pingshen =  data.pingshen;
			formData.value.shending =  data.shending;
			formData.value.difference =  data.difference;
			formData.value.zhongbiao =  data.zhongbiao;
			formData.value.check_date =  data.check_date;
			formData.value.is_weituo =  data.is_weituo;
			formData.value.mark =  data.mark;
			selectRow.value  = data.engine;	
			dataInfo.value = res.data;
			
		}).finally(() => {
			loading2.value = false;
		})
	}
	
	onMounted(() => {
		
	});
</script>

<style lang="scss" scoped>

</style>